import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';


/// 球脉冲Footer
class CustomLottieFooter extends Footer {
  /// Key
  final Key? key;

  /// 颜色
  final Color? color;

  /// 背景颜色
  final Color? backgroundColor;

  final LinkFooterNotifier linkNotifier = LinkFooterNotifier();

  CustomLottieFooter({
    this.key,
    this.color = Colors.blue,
    this.backgroundColor = Colors.transparent,
    bool enableHapticFeedback = true,
    bool enableInfiniteLoad = false,
    bool overScroll = false,
  }) : super(
          extent: 60.0,
          triggerDistance: 60.0,
          float: false,
          enableHapticFeedback: enableHapticFeedback,
          enableInfiniteLoad: enableInfiniteLoad,
          completeDuration: const Duration(seconds: 1),
        );

  @override
  Widget contentBuilder(
      BuildContext context,
      LoadMode loadState,
      double pulledExtent,
      double loadTriggerPullDistance,
      double loadIndicatorExtent,
      AxisDirection axisDirection,
      bool float,
      Duration? completeDuration,
      bool enableInfiniteLoad,
      bool success,
      bool noMore) {
    // 不能为水平方向
    assert(
        axisDirection == AxisDirection.down ||
            axisDirection == AxisDirection.up,
        'Widget cannot be horizontal');
    linkNotifier.contentBuilder(
        context,
        loadState,
        pulledExtent,
        loadTriggerPullDistance,
        loadIndicatorExtent,
        axisDirection,
        float,
        completeDuration,
        enableInfiniteLoad,
        success,
        noMore);
    return CustomLottieFooterWidget(
      key: key,
      color: color,
      backgroundColor: backgroundColor,
      linkNotifier: linkNotifier,
    );
  }
}

/// 球脉冲组件
class CustomLottieFooterWidget extends StatefulWidget {
  /// 颜色
  final Color? color;

  /// 背景颜色
  final Color? backgroundColor;

  final LinkFooterNotifier linkNotifier;

  const CustomLottieFooterWidget({
    Key? key,
    this.color,
    this.backgroundColor,
    required this.linkNotifier,
  }) : super(key: key);

  @override
  CustomLottieFooterWidgetState createState() {
    return CustomLottieFooterWidgetState();
  }
}

class CustomLottieFooterWidgetState extends State<CustomLottieFooterWidget> with SingleTickerProviderStateMixin{
    LoadMode get _refreshState => widget.linkNotifier.loadState;

    double get _indicatorExtent => widget.linkNotifier.loadIndicatorExtent;

    bool get _noMore => widget.linkNotifier.noMore;

    late final AnimationController lottieController;

    @override
    void initState() {
        lottieController = AnimationController(vsync: this)..addStatusListener((status) {
            if(status==AnimationStatus.completed){
                lottieController.reset();
            }
        });
        super.initState();
    }


    @override
    Widget build(BuildContext context) {
        if (_noMore) return Container(
            color: Colors.white,
            child: Center(
                child: Text("没有更多了",style: TextStyle(fontSize:13,color: Colors.grey)),
            ),
        );
        String text="";
        if(_refreshState==LoadMode.drag){
            text="上拉加载更多";
        }
        if(_refreshState==LoadMode.armed||_refreshState==LoadMode.load){
            lottieController.repeat();
            text="正在加载";
        }
        if(_refreshState==LoadMode.loaded){
            text=_noMore?"没有更多了":"加载完成";
        }
        if(_refreshState==LoadMode.inactive){
            text="上拉加载更多";
            lottieController.reset();
        }
        return Container(
            child: Center(
                child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                        Lottie.asset(
                            'lib/assets/lottie/loading-1.json',
                            width: 40,
                            height: 40,
                            controller: lottieController,
                            onLoaded: (composition){
                                lottieController..duration = composition.duration..reset();
                            },
                        ),
                        SizedBox(width: 0),
                        Text(text,style: TextStyle(fontSize:13,color: Colors.grey))
                    ],
                ),
            ),
        );
    }
}
